var lottery = require('./code/lottery/lottery');
var fs = require("fs");
var path = require("path");
var query = require("./db/db");

var id = 770706;
function def1() {
    function def2() {
        id += 1;
        return id;
    }

    return def2;
}
var ctrl = def1();
function pk10day(thisid) {
    function realtime(b) {
        var stopday = 7;
        var thattime = '2016-01-01 09:05';//计算标准
        var thatid = 736330;
        var plustime;
        if (b - thatid > 178) {
            xx = Math.floor((b - thatid) / 179) + stopday;//过了多少天
            yy = (b - thatid) % 179;             //当天的第几期
            plustime = yy * 5 + (60 * 24) * xx;
        } else {
            plustime = (b - 736330) * 5;
        }
        var time = new Date(thattime.replace("-", "/"));
        time.setMinutes(time.getMinutes() + plustime, time.getSeconds(), 0);
        var year = time.getFullYear().toString();
        var month = (time.getMonth() + 1).toString();
        if (month < 10) {
            month = '0' + month
        }
        var day = time.getDate().toString();
        if (day < 10) {
            day = '0' + day
        }
        var hour = time.getHours().toString();
        if (hour < 10) {
            hour = '0' + hour
        }
        var mins = time.getMinutes().toString();
        if (mins < 10) {
            mins = '0' + mins
        }
        time = year + '-' + month + '-' + day + ' ' + hour + ':' + mins;
        return (time)
    }

    var sql1 = 'select count(*) from result where gameid = "' + thisid + '";';
    query(sql1, function (err, vals) {
        if (err) throw err;
        if (vals.length) {
            var sql2 = 'update result set opentime = "' + realtime(thisid) + '" where gameid = "' + thisid + '" and gamekind = "bjkl";';
            query(sql2, function (err, vals) {
                if (err) throw err;
                console.log(vals);
                setTimeout(init, 2000)
            })
        } else {
            console.log('库中无该期')
            setTimeout(init, 2000)
        }
    })

}


function init() {
    var thisgameid = ctrl();
    console.log('——————————————————分割线——————————————————');
    console.log('修改期数为:' + thisgameid);
    pk10day(thisgameid);
}

init();
/*setInterval(init, 2000000);*/


process.on('uncaughtException', function (err) {
    console.log(err.stack);
});
